package ods_supplier

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"server/global"
	"server/model/common/request"
	"server/model/common/response"
)

type SupplierApi struct {
}

var odsSupplierService = SupplierService{}



func (odsSupplierApi *SupplierApi) CreateOdsSupplier(c *gin.Context) {
	var odsSupplier OdsSupplier
	err := c.ShouldBindJSON(&odsSupplier)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if err := odsSupplierService.CreateOdsSupplier(&odsSupplier); err != nil {
		global.Logger.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}


func (odsSupplierApi *SupplierApi) DeleteOdsSupplier(c *gin.Context) {
	var odsSupplier OdsSupplier
	err := c.ShouldBindJSON(&odsSupplier)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsSupplierService.DeleteOdsSupplier(odsSupplier); err != nil {
		global.Logger.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}


func (odsSupplierApi *SupplierApi) DeleteOdsSupplierByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsSupplierService.DeleteOdsSupplierByIds(IDS); err != nil {
		global.Logger.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}


func (odsSupplierApi *SupplierApi) UpdateOdsSupplier(c *gin.Context) {
	var odsSupplier OdsSupplier
	err := c.ShouldBindJSON(&odsSupplier)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := odsSupplierService.UpdateOdsSupplier(odsSupplier); err != nil {
		global.Logger.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}


func (odsSupplierApi *SupplierApi) FindOdsSupplier(c *gin.Context) {
	var odsSupplier OdsSupplier
	err := c.ShouldBindQuery(&odsSupplier)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reodsSupplier, err := odsSupplierService.GetOdsSupplierById(odsSupplier.ID); err != nil {
		global.Logger.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reodsSupplier": reodsSupplier}, c)
	}
}


func (odsSupplierApi *SupplierApi) GetOdsSupplierList(c *gin.Context) {
	var pageInfo OdsSupplierSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := odsSupplierService.GetOdsSupplierInfoList(pageInfo); err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}


func (odsSupplierApi *SupplierApi) GetAllOdsSuppliers(c *gin.Context) {
	odsSupplier, err := odsSupplierService.GetAllOdsSuppliers()
	if err != nil {
		global.Logger.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"odsSupplier": odsSupplier}, "获取成功", c)
	}
}

func (odsSupplierApi *SupplierApi) GetOdsSupplierColLabel(c *gin.Context) {
	colLabel := GetColLabel(OdsSupplier{})
	response.OkWithData(gin.H{"colLabel": colLabel}, c)
}
